package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import DataObjects.Payment;

public class AddNewPaymentOfTable {
	
	private Payment pay;
	private int status=0;
	
	
	public AddNewPaymentOfTable(Payment pay)
	{
		this.pay=pay;
		
	}
	
	public void savePaymentOfTable ()
	{
		SQLConnection sc=new SQLConnection();
		
		try {
			
			PreparedStatement ps = sc.getConnection().prepareStatement("INSERT INTO payment (date,time,numtable,amount,typeOfPayment,changeOfPayment,orderid,numPresentCart,tips) values(?,?,?,?,?,?,?,?,?);");
			PreparedStatement ps1 = sc.getConnection().prepareStatement("UPDATE client_club  SET balance=?  WHERE numCard=?;");
			sc.getConnection().setAutoCommit(false);
			ps.setString(1, pay.getDate());
			ps.setString(2, pay.getTime());
			ps.setInt(3, pay.getNumtable());
			ps.setFloat(4, pay.getAmount());
			ps.setString(5, pay.getTypePayment());
			ps.setFloat(6, pay.getChange());
			ps.setFloat(7, pay.getOrderid());
			
			
			if(pay.getTypePayment().equals("כרטיס מתנה"))
			{
				ps.setInt(8, pay.getNumPresentCard());
				ps1.setFloat(1, pay.getBalance());
				ps1.setInt(2, pay.getNumPresentCard());
				ps1.executeUpdate();
			}
			else
				ps.setInt(8, 0000);
			ps.setFloat(9, pay.getTips());
			ps.executeUpdate();
									
			sc.getConnection().commit();
			ps.close();
			ps1.close();
			sc.getConnection().close();	
			 status=1;	
		} catch (SQLException e) {
			status=0;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		System.out.println("AAA");
			
	}
	

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

}
